package com.bw.miaosha.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bw.miaosha.entity.MiaoshaActivityEntity;
import com.bw.miaosha.service.MiaoshaActivityService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 
 *
 * @author jwcz
 * @email 2898807664@qq.com
 * @date 2022-02-22 19:12:57
 */
@RestController
@RequestMapping("miaosha/miaoshaactivity")
public class MiaoshaActivityController {
    @Autowired
    private MiaoshaActivityService miaoshaActivityService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = miaoshaActivityService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/slist")
    public R slist(@RequestParam Map<String, Object> params){
        return miaoshaActivityService.slist(params);
    }

    @RequestMapping("/getStock")
    public MiaoshaActivityEntity getStock(@RequestParam("userId")Long userId) {
        QueryWrapper<MiaoshaActivityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userId);
        return miaoshaActivityService.getOne(wrapper);
    }
    //feign调用秒杀服务，根据商品id查询商品对象
    @RequestMapping("/getVO")
   public MiaoshaActivityEntity getVO(@RequestParam("goodsId")Integer goodsId){
        QueryWrapper<MiaoshaActivityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("goodsId",goodsId);
        return  miaoshaActivityService.getOne(wrapper);

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		MiaoshaActivityEntity miaoshaActivity = miaoshaActivityService.getById(id);

        return R.ok().put("miaoshaActivity", miaoshaActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.save(miaoshaActivity);
		//删除redis，重新加载商品库存信息存入到redis中
        redisTemplate.delete("goodsList");
        //把库存存入redis
        Integer stockCount = miaoshaActivity.getStockCount();
        redisTemplate.boundValueOps("stockCount").set(String.valueOf(stockCount));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.updateById(miaoshaActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		miaoshaActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
